<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->model('filters_model');
		$this->load->model('seasons_model');
		$this->load->model('items_model');
		$this->load->helper('url');
		$this->load->helper('html');
	}
	
	public function index( $filter_size = 0, $price_from = null, $price_to = null, $item_name = null, $item_code = null, $filter_season = 0 )
	{
		$this->load->helper('form');
		
		if ( ! file_exists(APPPATH.'/views/home.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}

		$data = $this->input->post();
		
		$data['title'] = 'Пошук товарів'; // Capitalize the first letter
		$data['filters'] = $this->filters_model->get_filters();
		$data['seasons'] = $this->seasons_model->get_seasons();
		
		unset($data['filter_season'][0]);
		
		if( empty( $filter_size ) ) {
			$filter_size = ( empty( $data['filter_size'] ) ) ? 0 : $data['filter_size'];
		} else {
			$size_by_get = array();
			$filter_size = explode( '.', $filter_size );
			foreach( $data['filters'] as $_key=>$_vals ) {
				if( $_vals['value_to'] > 0 ) {
					if( array_search( $_vals['value'].'-'.$_vals['value_to'], $filter_size ) !== false ) {
						$size_by_get[$_key+1] = $_vals['value'].'-'.$_vals['value_to'];
					}
				} else {
					if( array_search( $_vals['value'], $filter_size ) !== false ) {
						$size_by_get[$_key+1] = $_vals['value'];
					}
				}
			}
			$filter_size = $size_by_get;
		}
		
		if( empty( $filter_season ) ) {
			$filter_season = ( empty( $data['filter_season'] ) ) ? 0 : $data['filter_season'];
		} else {
			$season_by_get = array();
			$filter_season = explode( '.', $filter_season );
			foreach( $data['seasons'] as $_key=>$_vals ) {
				if( array_search( $_vals['id'], $filter_season ) !== false ) {
					$season_by_get[$_vals['id']] = $_vals['id'];
				}
			}
			$filter_season = $season_by_get;
		}
		
		$price_from = ( !is_null( $price_from )) ? $price_from : (( empty( $data['price_from'] ) ) ? '' : trim( $data['price_from'] ));
		$price_to = ( !is_null( $price_to )) ? $price_to : (( empty( $data['price_to'] ) ) ? '' : trim( $data['price_to'] ));
		$item_name = ( !is_null( $item_name ) && $item_name != -1 ) ? urldecode( $item_name ) : (( empty( $data['item_name'] ) ) ? '' : trim( $data['item_name'] ));
		$item_code = ( !is_null( $item_code ) && $item_code != -1 ) ? urldecode( $item_code ) : (( empty( $data['item_code'] ) ) ? '' : trim( $data['item_code'] ));
		$price_from = 1 * $price_from;
		$price_to = 1 * $price_to;
		
		
		$data['price_from'] = ( $price_from > 0 ) ? $price_from : '';
		$data['price_to'] = ( $price_to > 0 ) ? $price_to : '';
		$data['item_name'] = $item_name;
		$data['item_code'] = $item_code;
		$data['filter_size'] = $filter_size;
		$data['filter_season'] = $filter_season;
		
		if( !empty( $data['filter_size'] ) || !empty( $data['price_from'] ) || !empty( $data['price_to'] ) || !empty( $data['item_name'] ) || !empty( $data['item_code'] ) || !empty( $data['filter_season'] ))
		{
			//launch search
			$data['items'] = $this->items_model->get_items( $data['filter_size'], $data['price_from'], $data['price_to'], $data['item_name'], $data['item_code'], $data['filter_season'] );
		}
		
		if( count($data['seasons']) == count($data['filter_season'])) {
			$data['filter_season'][0] = 1;
		}
		
		$data['assets'] = $this->assetsScan();
		
		$this->load->view('templates/header', $data);
		$this->load->view('home', $data);
		$this->load->view('templates/footer', $data);
	}
	
	public function search()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
	}
	
	private function assetsScan() {
	
		$result = array();
		$dir = 'assets';
		$cdir = scandir( $dir );
		foreach ($cdir as $key => $value) {
			if( !in_array( $value, array( ".", ".." ))) {
				if( is_dir( $dir . '/' . $value )) {
					$result[$value] = dirToArray($dir . '/' . $value);
				} else {
					$result[] = $value;
				}
			}
		}
		return $result;
	}
}

